<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Messenger/classes/PhpFox_ComponentView.class.php, $Author:  $ */



class PhpFox_Mod_Messenger_ComponentView extends BaseComponent
{
    function process()
    {
        $oReq = &App::getRequest();
        $oSrvMessenger = $this->_oModule->getService('Messenger');
        $oSrvAccount = &App::getModuleService('Account', 'Security');
        $sType = 'mail';

        $aArg = App::getParam('seen');
    	if($oReq->getInt('crush'))
        {
        	if($oReq->getAction() == 'Guess')
            {
                $oUrl = App::getUrl();
                if($oSrvMessenger->checkGuess($oReq->get('user'), $oReq->getInt('crush')))
                {
                	$oSrvMessenger->setViewedStatus($oReq->getInt('crush'), $aArg['guess']);
                	App::forward($oUrl->getUrl(), App::format('Mod_Messenger.right.guess', array($oReq->get('user'), App::makeUrl('public.account.messages.sendcrush').'user_'.$oReq->get('user'), $oReq->get('user'))), 'message', App::getPageName(), 'ViewCr');
                }
                else
                {
                    $this->addErrors(App::format('Mod_Messenger.wrong.guess', array($oUrl->getUrl().'#guess')));
                }
            }
            $sBoxName = App::format('Mod_Messenger.view_crush_alert');
            $aMessage = $oSrvMessenger->getMessage($oReq->getInt('crush'));
            if(((strtolower($oSrvAccount->getCurrentUserLogin()) != strtolower($aMessage['to'])) && (strtolower($oSrvAccount->getCurrentUserLogin()) != strtolower($aMessage['from']))))
                App::gotoUrl('public.account.messages.crush');

            $aMessage['text'] = $aMessage['text'];
            if(!intval($aMessage['seen']))
            	$oSrvMessenger->setViewedStatus($oReq->getInt('crush'), $aArg['viewed']);
            $sType    = 'crush';
            $aMessage['to'] = strtolower($aMessage['to']);
        }

        if($oReq->getInt('id'))
        {
            $sBoxName = App::format('Mod_Messenger.view_mail');
            $aMessage = $oSrvMessenger->getMessage($oReq->getInt('id'));
            $aMessage['title'] = htmlspecialchars($aMessage['title']);
            if((($oSrvAccount->getCurrentUserLogin() != $aMessage['to']) && ($oSrvAccount->getCurrentUserLogin() != $aMessage['from'])))
                App::gotoUrl('public.account.messages');
            $bIsInbox = ($oSrvAccount->getCurrentUserLogin() == $aMessage['to']) ? true : false;
            $aNav = $oSrvMessenger->getAllMessages($oReq->getInt('id'), $oSrvAccount->getCurrentUserLogin(), 'mail', $bIsInbox);
            if(!intval($aMessage['seen']) && (strtolower($oSrvAccount->getCurrentUserLogin()) == strtolower($aMessage['to'])))
                $oSrvMessenger->setViewedStatus($oReq->getInt('id'), $aArg['viewed']);
        }
        if($oReq->getInt('del'))
        {
            $aMessage = $oSrvMessenger->getMessage($oReq->getInt('del'));
            
            $oSrvMessenger->markDel($oReq->getInt('del'),$oSrvAccount->getUserIdByName($oSrvAccount->getCurrentUserLogin()));

            if($oSrvAccount->getCurrentUserLogin() == $aMessage['from'])
                App::gotoUrl('public.account.messages', ($oReq->get('box') == 'sentbox' ? array('box'=>'sentbox') : array()), array(App::format('Mod_Messenger.deleted.items')));
            else
                App::gotoUrl('public.account.messages', array(), array(App::format('Mod_Messenger.deleted.items')));
        }

        $oTpl = &$this->_oModule->getTemplate();
		$oTpl->assignSrc(array(
		                      'aErrors' => $this->getErrors(),
		                      'aMess' 	=> $aMessage,
	                      ));

        $oTpl->assign(array(

                             'sBoxName'	=> $sBoxName,
                             'sType'	=> $sType,
        					 'sImageUrl'=> App::getParam('user.pic.url'),
        					 'sUser'	=> strtolower($oSrvAccount->getCurrentUserLogin()),
                             'bIsSent'  => ($oSrvAccount->getCurrentUserLogin() == $aMessage['from']) ? true : false,
                             'aNav'     => !empty($aNav) ? $aNav : false,
                             'sBox' => $oReq->get('box')
        ));

        $sHtml = $oTpl->fetch('View.html');

        return $sHtml;
    }
}
?>